Completed
Push — master ( 21cf19...bbcb64 )
by Ankit
03:20
created

message.js ➔ composeChoose   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 16
rs 9.4285
c 1
b 0
f 1
1
2
// Websocket Connection Open
3
var conn = new WebSocket("ws://localhost:8080");
0 ignored issues
show
Bug introduced by
The variable WebSocket seems to be never declared. If this is a global, consider adding a /** global: WebSocket */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
4
5
6
// For First time
7
function init() {
8
  conn.send("OpenChat initiated..!");
9
}
10
11
conn.onopen = function() {
12
  console.log("Connection established!");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
13
  init();
14
};
15
16
// SideBar Load Request
17
function sidebarRequest() {
18
  conn.send("Load Sidebar");
19
}
20
21
// Create Sidebar
22
function createSidebarElement(data, eleId)
23
{
24
  // organising content according to time
25
  $("#"+eleId).html("");
26
  var condition = data.length;
27
  for (var i = 0; i < condition; i++)
28
  {
29
30
    var div1 = $("<div></div>").addClass("row sideBar-body");
31
32
    div1.attr({
33
      "id" : data[i].username
34
    });
35
36
    var div2 = $("<div></div>").addClass("col-sm-3 col-xs-3 sideBar-avatar");
37
    var div3 = $("<div></div>").addClass("avatar-icon");
38
    var imgElement = $("<img>").attr({
39
      "src": "../public/assests/img/bg.png"
40
    });
41
    div3.append(imgElement);
42
    div2.append(div3);
43
    div1.append(div2);
44
45
    div2 = $("<div></div>").addClass("col-sm-9 col-xs-9 sideBar-main");
46
    div3 = $("<div></div>").addClass("row");
47
    var div4 = $("<div></div>").addClass("col-sm-8 col-xs-8 sideBar-name");
48
    var spanE = $("<span></span>").addClass("name-meta").text(data[i].name);
49
    div4.append(spanE);
50
    div3.append(div4);
51
52
    div4 = $("<div></div>").addClass("col-sm-4 col-xs-4 pull-right sideBar-time");
53
    spanE = $("<span></span>").addClass("time-meta pull-right").text(data[i].time);
54
     div4.append(spanE);
55
    div3.append(div4);
56
    div2.append(div3);
57
58
    div1.append(div2);
59
    $("#"+eleId).append(div1);
60
61
    if(eleId === "message" && $("#text_reply").attr("name") == data[i].login_id)
62
    {
63
      $("#"+data[i].username).addClass("active");
64
    }
65
  }
66
}
67
68
69
// For updating Sidebar
70
function SideBar(msg) {
71
  // Getting Div
72
  if (msg != null) {
73
    createSidebarElement(msg, "message");
74
  }
75
}
76
77
function toConversation() {
78
  $(".side").addClass("hide");
79
  $(".message").addClass("show");
80
  $(".lowerBar").addClass("show");
81
  $(".reply-emojis").addClass("hide");
82
  $(".reply-recording").addClass("hide");
83
}
84
85
function toSidebar() {
86
  $(".side").removeClass("hide");
87
  $(".message").removeClass("show");
88
  $(".lowerBar").removeClass("show");
89
  $(".reply-emojis").removeClass("hide");
90
  $(".reply-recording").removeClass("hide");
91
}
92
93
function width() {
94
  if (window.innerWidth < 768) {
95
    return true;
96
  }
97
  return false;
98
}
99
100
// Creating new Conversation or Loading Conversation
101
function newConversation(element, load)
102
{
103
104
  if(width())
105
  {
106
    toConversation();
107
  }
108
109
  var msg = {
110
    "username": element.id,
111
    "load": load,
112
    "newConversation": "Initiated"
113
  };
114
  conn.send(JSON.stringify(msg));
115
}
116
117
// Set Details
118
function setConversationDetails(details)
119
{
120
  $(".heading-name-meta").text(details.name);
121
  $(".heading-name-meta").attr({
122
    "href": location.href.substring(0, location.href.lastIndexOf("/") + 1) + "account.php/" + details.username
123
  });
124
125
  $(".heading-online").removeClass("show");
126
  if (details.login_status === "1") {
127
    $(".heading-online").addClass("show");
128
  }
129
130
  $("#text_reply").attr({
131
    "name": details.id
132
  });
133
}
134
135
136
// Update Current Conversation
137
function updateConversation(data)
138
{
139
140
  var ele = document.getElementById("conversation");
141
  ele.innerHTML = "";
142
143
  if (data[0].type === 1) {
144
    // For showing previous message
145
    if (data[0].load > 10)
146
    {
147
      var divE1 = $("<div></div>").addClass("row message-previous");
148
      var divE2 = $("<div></div>").addClass("col-sm-12 previous");
149
      var aElement = $("<a></a>").text("Show Previous Message!");
150
      aElement.attr({
151
        "id": data[0].username,
152
        "name": data[0].load
153
      });
154
      divE2.append(aElement);
155
      divE1.append(divE2);
156
      $("#conversation").append(divE1);
157
    }
158
159
    for (var i = data.length - 1; i >= 1; i--) {
160
      // create element
161
      var divElement1 = $("<div></div>").addClass("row message-body");
162
      var divElement2 = $("<div></div>").addClass("col-sm-12");
163
      var divElement3 = $("<div></div>");
164
      var messageText = $("<div></div>").addClass("message-text").text(data[i].message);
165
      var spanElement = $("<span></span>").addClass("message-time pull-right").text(data[i].time);
166
167
      if (data[i]["sent_by"] !== data[i].start)
168
      {
169
       divElement2.addClass("message-main-receiver");
170
       divElement3.addClass("receiver");
171
      }
172
      else
173
      {
174
        divElement2.addClass("message-main-sender");
175
       divElement3.addClass("sender");
176
      }
177
      divElement3.append(messageText);
178
      divElement3.append(spanElement);
179
      divElement2.append(divElement3);
180
      divElement1.append(divElement2);
181
      $("#conversation").append(divElement1);
182
    }
183
184
    setConversationDetails(data[0]);
185
186
    ele.scrollTop = ele.scrollHeight;
187
  } else {
188
    setConversationDetails(data[0]);
189
  }
190
}
191
192
// For reply to other messages
193
function reply() {
194
195
  var message = [$("#text_reply").val()];
196
  var id = $("#text_reply").attr("name");
197
  $("#text_reply").val("");
198
  // console.log(message);
199
  var q = {
200
    "name": id,
201
    "reply": message
202
  };
203
  conn.send(JSON.stringify(q));
204
}
205
206
function notFound(eleId)
207
{
208
  eleId = "#"+eleId;
209
  $(eleId).text("");
210
  var divElement = $("<div></div>").addClass("notFound").text("Not Found");
211
  $(eleId).append(divElement);
212
}
213
214
function composeChoose() {
215
  var text = document.getElementById("composeText").value;
216
  if (text !== "")
217
  {
218
    var msg =
219
    {
220
      "value": text,
221
      "Compose": "Compose"
222
    };
223
    conn.send(JSON.stringify(msg));
224
  }
225
  else
226
  {
227
    $("#compose").html("");
228
  }
229
}
230
231
//compose messages
232
function composeResult(arr)
233
{
234
  var ele = document.getElementById("compose");
235
  ele.innerHTML = "";
236
237
  if (arr !== "Not Found")
238
  {
239
    createSidebarElement(arr, "compose");
240
  }
241
  else
242
  {
243
    notFound("compose");
244
  }
245
}
246
247
function searchChoose() {
248
  var text = $("#searchText").val();
249
  if (text !== "") {
250
    var msg = {
251
      "value": text,
252
      "search": "search"
253
    };
254
255
    conn.send(JSON.stringify(msg));
256
  }
257
  else
258
  {
259
    sidebarRequest();
260
  }
261
262
}
263
264
function searchResult(arr) {
265
  if (arr !== "Not Found") {
266
    createSidebarElement(arr, "message");
267
  }
268
  else
269
  {
270
    notFound("message");
271
  }
272
273
}
274
275
// Load previous messages
276
function previous(element)
277
{
278
  var user = element.id;
0 ignored issues
show
Unused Code introduced by
The variable user seems to be never used. Consider removing it.
Loading history...
279
  var lo = element.name;
280
  newConversation(element, lo);
281
}
282
283
function hideComposeScreen() {
284
  $(".side-two").css({
285
    "left": "-100%"
286
  });
287
}
288
289
// Audio Recognization
290
291
function startDictation() {
292
293
  if (window.hasOwnProperty("webkitSpeechRecognition")) {
294
295
    var recognition = new webkitSpeechRecognition();
0 ignored issues
show
Coding Style Best Practice introduced by
By convention, constructors like webkitSpeechRecognition should be capitalized.
Loading history...
Bug introduced by
The variable webkitSpeechRecognition seems to be never declared. If this is a global, consider adding a /** global: webkitSpeechRecognition */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
296
297
    recognition.continuous = false;
298
    recognition.interimResults = false;
299
300
    recognition.lang = "en-IN";
301
    recognition.start();
302
303
    recognition.onresult = function(e) {
304
      document.getElementById("text_reply").value = e.results[0][0].transcript;
305
      recognition.stop();
306
      reply();
307
    };
308
309
    recognition.onerror = function() {
310
      recognition.stop();
311
    }
312
313
  }
314
}
315
316
317
// On Message
318
conn.onmessage = function(e)
319
{
320
  var msg = JSON.parse(e.data);
321
  console.log(msg);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
322
  if (!width())
323
  {
324
    SideBar(msg.sidebar);
325
  } else
326
  {
327
    if (!$(".side").hasClass("hide")) {
328
      SideBar(msg.sidebar);
329
    }
330
  }
331
332
  if (msg.initial !== undefined) {
333
    SideBar(msg.initial);
334
  }
335
336
  if (msg.conversation !== undefined) {
337
    updateConversation(msg.conversation);
338
  }
339
340
  if (msg.reply !== undefined) {
341
    var textAreaId = $("#text_reply").attr("name");
342
    if (msg.reply[0].id === textAreaId) {
343
      updateConversation(msg.reply);
344
    }
345
  }
346
347
  if (msg.Search !== undefined) {
348
    searchResult(msg.Search);
349
  }
350
351
  if (msg.Compose !== undefined) {
352
    composeResult(msg.Compose);
353
  }
354
};
355
356
// Event Listeners
357
$(document).ready(function(){
358
  $("body").on("click", ".sideBar-body", function() {
359
    newConversation(this,10);
360
    hideComposeScreen();
361
  });
362
363
  $("body").on("click", ".reply-send",
364
   function() {
365
    reply();
366
  });
367
368
  $("body").on("click", ".reply-recording",
369
   function() {
370
    startDictation();
371
  });
372
373
  $("body").on("click", ".lowerBar-recording",
374
   function() {
375
    startDictation();
376
  });
377
378
  $("body").on("click", ".lowerBar-back",
379
   function() {
380
    toSidebar();
381
    sidebarRequest();
382
  });
383
384
  $("body").on("click", ".previous a",
385
   function() {
386
    previous(this);
387
  });
388
389
  $("body").on("keyup", "#searchText",
390
   function() {
391
    searchChoose();
392
  });
393
394
  $("body").on("keyup", "#composeText",
395
   function() {
396
    composeChoose();
397
  });
398
399
  $(".heading-compose").click(function() {
400
    $(".side-two").css({
401
      "left": "0"
402
    });
403
  });
404
405
  $(".newMessage-back").click(function() {
406
    hideComposeScreen();
407
  });
408
409
});
410
411
console.log("Hello, Contact me at [email protected]");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...